<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\wxapp\controller;

use api\wxapp\controller\ApiBaseController;
use think\Db;
use api\wxapp\model\Shop;
use api\wxapp\model\ShopGoods;
use api\wxapp\model\GoodsCate;
use api\wxapp\model\Goods;
class GoodsController extends ApiBaseController
{
    //商品列表
    public function index(Goods $goods)
    {
        $where = array();
        $params = $this->getParam();
        $this->checkshopid($params);
        $where[] = ['t1.shop_id','eq',$params['shop_id']];
        $where[] = ['t1.status','eq',2];//正常在收商品
        if(isset($params['cate_id'])){
            //此分类是一级分类还是二级分类
            $cate = GoodsCate::get($params['cate_id']);
            if($cate['pid']){
                //当前是二级分类
                $idarr = Goods::where('cateId',$params['cate_id'])->column('id'); 
            }else{
                //一级分类
                $map[] = ['cateId','in',GoodsCate::where('pid',$params['cate_id'])->column('id')];
                $idarr = Goods::where($map)->column('id'); 
            }
            $where[] = ['t1.goods_id','in',$idarr];
        }
        if(isset($params['name'])){
            $where[] = ['t2.name','like','%'.$params['name'].'%'];
        }
        $list = $goods->getList($where);
        $this->success('请求成功!',$list);
    }
    //商品管理--商品分类
    public function cate()
    {
        $list = GoodsCate::where('pid',0)->all()
        ->each(function($item,$key){
            $item['son'] = GoodsCate::where('pid',$item['id'])->select();
        });
        $this->success('请求成功!',$list);
    }
    //商品详情
    public function info(Goods $goods)
    {
        $params = $this->getParam();
        $this->checkgoodsId($params);
        $this->checkshopid($params);
        $this->success('请求成功!',$goods->info($params));
    }
}
